package com.xbao.core.util.pub.tool;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class SecDateUtil {

	/**
	 * 将时间字符串转换成日期类型（精确度：秒）
	 * 
	 * @param time
	 * @return Date
	 */
	public static Date getDateByString(String time) {
		Date date = null;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			date = formatter.parse(time);
		} catch (Exception e) {
			System.out.println("error --StringUtil.getDateByString   ");
		}
		return date;
	}

	public static Date getDateByTime(Long time) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String d = format.format(time);
		Date date=null;
		try {
			date = format.parse(d);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		System.out.println("Format To String(Date):" + d);
		System.out.println("Format To Date:" + date);
		return date;
	}

	public static Long daysBetween(Long begintime,Long endtime)
	{
		try {
			long between_days = (endtime - begintime) / (1000 * 3600 * 24);

			return Long.parseLong(String.valueOf(between_days));
		}catch (Exception e){
			e.printStackTrace();
		}
		return 0l;
	}


	public static int daysBetween(Date smdate,Date bdate) throws ParseException
	{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		smdate=sdf.parse(sdf.format(smdate));
		bdate=sdf.parse(sdf.format(bdate));
		Calendar cal = Calendar.getInstance();
		cal.setTime(smdate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(bdate);
		long time2 = cal.getTimeInMillis();
		long between_days=(time2-time1)/(1000*3600*24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 *字符串的日期格式的计算
	 */
	public static int daysBetween(String smdate,String bdate) {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(sdf.parse(smdate));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long time1 = cal.getTimeInMillis();
		try {
			cal.setTime(sdf.parse(bdate));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long time2 = cal.getTimeInMillis();
		long between_days=(time2-time1)/(1000*3600*24);

		return Integer.parseInt(String.valueOf(between_days));
	}
	/**
	 * 将时间字符串转换成日期类型（精确度：日）
	 * 
	 * @param time
	 * @return
	 */
	public static Date getDayByString(String time) {
		Date date = null;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		try {
			date = formatter.parse(time);
		} catch (Exception e) {
			System.out.println("error --StringUtil.getDateByString   ");
		}
		return date;
	}

	/**
	 * 将日期字符串转换成 "yyyy-MM-dd HH:mm:ss" 字符串
	 * 
	 * @param time
	 * @return String
	 */
	public static String getStringDateByStringDate(String time) {
		Date date = getDateByString(time);
		String datestr = null;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			datestr = formatter.format(date);
		} catch (Exception e) {
			System.out.println("error --StringUtil.getDateByString   ");
		}
		return datestr;
	}

	/**
	 * 将日前数值转换成字符串
	 * 
	 * @param date
	 * @return String
	 */
	public static String getStringDateByDate(Date date) {
		String datestr = null;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			datestr = formatter.format(date);
		} catch (Exception e) {
			System.out.println("error --StringUtil.getDateByString   ");
		}
		return datestr;
	}

	/**
	 * 将时间字符串累加一天
	 * 
	 * @param time
	 * @return String
	 */
	public static String getStringDayAddDay(String time) {
		Date date = null;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		try {
			date = formatter.parse(time);
		} catch (Exception e) {
			System.out.println("error --StringUtil.getDateByMoreDay   ");
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DATE, 1); // 日期分钟加1,Calendar.DATE(天),Calendar.HOUR(小时)
		date = c.getTime(); // 结果
		String str = formatter.format(date);
		return str;

	}

	/**
	 * 将时间转成到天的时间字符串
	 * 
	 * @param date
	 * @return String
	 */
	public static String getStringDayByDate(Date date) {
		String datestr = null;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		datestr = formatter.format(date);
		return datestr;
	}

	/**
	 * 将时间对象转换成时间戳对象
	 * 
	 * @param date
	 * @return Timestamp
	 */
	public static Timestamp getTimestampByDate(Date date) {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Timestamp ts = Timestamp.valueOf(sf.format(date));
		return ts;
	}

	/**
	 * 时间戳对象转成成字符串
	 * 
	 * @param stamp
	 * @return String
	 */

	public static String getStringDateByTimestamp(Timestamp stamp) {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String ts = sf.format(stamp);
		return ts;
	}

	/**
	 * 获取加一个月时间的数据
	 * 
	 * @param date
	 * @return
	 */
	public static String addOneMonthDay(Date date) {
		Calendar calender = Calendar.getInstance();
		calender.setTime(date);
		calender.add(Calendar.MONTH, 1);
		Date monthday = calender.getTime();
		return getStringDayByDate(monthday);
	}

	/**
	 * 获取加任意天数的日期
	 * 
	 * @param date
	 * @param date
	 * @return
	 */
	public static Date addDays(Date date, long days) {
		long daytime = days * 24 * 60 * 60 * 1000;
		long time = date.getTime();
		long last = time + daytime;
		Date damore = new Date(last);
		return damore;
	}

}
